/// 知识转移测试 - 验证学习到的知识能否被对话系统使用
use neurosymbolic_ai::{AutonomousLearner, DictionaryData, ActiveLearningDialogue};

fn main() {
    println!("╔════════════════════════════════════════════════════════════╗");
    println!("║          知识转移测试                                      ║");
    println!("╚════════════════════════════════════════════════════════════╝\n");

    // 步骤1：AI学习词典
    println!("步骤1：AI学习词典...\n");
    let mut learner = AutonomousLearner::new();
    let dictionary = DictionaryData::create_sample();
    
    println!("📚 词典包含 {} 个词条", dictionary.entries.len());
    learner.learn_from_dictionary(&dictionary);
    
    let stats = learner.get_statistics();
    println!("\n✅ 学习完成！掌握 {} 个概念\n", stats.total_concepts);
    
    // 步骤2：转移知识
    println!("{}", "=".repeat(60));
    println!("步骤2：转移知识到对话系统...\n");
    
    let mut dialogue = ActiveLearningDialogue::new();
    
    // 统计转移前
    let before_count = dialogue.export_knowledge().len();
    println!("转移前对话系统知识量: {}", before_count);
    
    // 转移概念
    let mut transferred = 0;
    for (name, concept) in learner.reasoning_chain().knowledge_graph().concepts.iter() {
        dialogue.reasoning_chain_mut().knowledge_graph_mut().add_concept(
            name,
            &concept.description,
            "词典",
        );
        transferred += 1;
    }
    
    // 转移关系
    for relation in learner.reasoning_chain().knowledge_graph().get_all_relations() {
        dialogue.reasoning_chain_mut().knowledge_graph_mut().add_relation(relation);
    }
    
    let after_count = dialogue.export_knowledge().len();
    println!("转移后对话系统知识量: {}", after_count);
    println!("✅ 成功转移 {} 个概念\n", transferred);
    
    // 步骤3：测试对话
    println!("{}", "=".repeat(60));
    println!("步骤3：测试对话效果...\n");
    
    let test_questions = vec![
        "什么是人工智能",
        "什么是机器学习",
        "什么是深度学习",
        "什么是神经网络",
    ];
    
    let mut success_count = 0;
    for (i, question) in test_questions.iter().enumerate() {
        println!("测试 {}: {}", i + 1, question);
        let response = dialogue.chat(question);
        
        // 检查是否回答了问题（不是说"不了解"）
        if !response.contains("不太了解") && !response.contains("教教我") {
            println!("✅ 成功回答\n");
            success_count += 1;
        } else {
            println!("❌ 未能回答\n");
        }
    }
    
    // 总结
    println!("{}", "=".repeat(60));
    println!("测试结果:");
    println!("  成功: {}/{}", success_count, test_questions.len());
    println!("  成功率: {:.1}%", (success_count as f64 / test_questions.len() as f64) * 100.0);
    
    if success_count == test_questions.len() {
        println!("\n🎉 完美！知识转移成功，AI能回答所有问题！");
    } else if success_count > 0 {
        println!("\n⚠️  部分成功，需要继续改进关键词提取");
    } else {
        println!("\n❌ 知识转移失败，需要检查系统");
    }
}

